import urllib3
import json
from urllib.parse import urlencode

# 1.实例化PoolManage对象    保持socket开启
http = urllib3.PoolManager()

# 2.请求和响应
headers = {"header1": "pythonvip", "header2": 666}
fields = {"name": "fuhua", "password": "nice"}

response = http.request("GET", "http://httpbin.org/get", fields=fields, headers=headers)  # field在get的请求中会拼接在url后面
print("status", response.status)
print("headers", response.headers)
print("data", response.data)

print("-" * 100)

args = {"stu_name": "fukua", "学生": "浮夸"}
args = urlencode(args)
url = "http://httpbin.org/post?" + args

response = http.request("POST", url, fields=fields, headers=headers)
print("status", response.status)
print("headers", response.headers)
print("data", response.data)

print("-" * 100)

res_str=response.data.decode("utf-8")
print(type(res_str),res_str)
res_dict=json.loads(res_str)
print("-" * 100)
print(res_dict["form"]["name"])